﻿$(document).ready(function () {
    document.getElementById('chat').scrollTop = 9999999;

    setTimeout(updateChat, 1000);

    $("#chat-send-button").click(sendChat);
    $("#chat-send-text").keypress(function (data) {
        if (data.keyCode == 13) {
            data.preventDefault();
            sendChat();
        }
    });
});

function updateChat() {
    $.get("/api/updatechat", function (data) {
        $("#chat").html(data);
        document.getElementById('chat').scrollTop = 9999999;
    }).error(function () {
        $("#chat").html("<span style='color: #f00'>Disconnected</span>");
    });
    setTimeout(updateChat, 1000);
}

function sendChat() {
    $.post("/api/sendchat?message=[" + $("#logged-in-user").val() + "] " + $("#chat-send-text").val(), function () {
        $("#chat-send-text").val("");
        var element = document.getElementById("chat-send-text");
        element.focus();
        element.onblur = function () {
            setTimeout(function () {
                element.focus();
            }, 0);
        };
    });
}